<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/1/12
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>

<%--头部搜索栏--%>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户姓名:</label>
            <div class="layui-input-inline">
                <input type="text" name="realname" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">登陆名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="loginname"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">用户地址：</label>
            <div class="layui-input-inline">
                <input type="text" name="address"  autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户手机:</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">身份证号：</label>
            <div class="layui-input-inline">
                <input type="text" name="identity"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">性别:</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男" >
                <input type="radio" name="sex" value="0" title="女">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i>查询</button>
            <button type="reset" class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-close"></i>重置</button>
        </div>
    </div>
</form>


<table id="test" lay-filter="test"></table>

<%--头部工具栏--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="dels">批量删除</button>
       </div>
</script>

<%--添加的form--%>
<form class="layui-form" style="display: none;width: 400px;height: 400px" id="UserAdd" lay-filter="UserAdd" >
    <div class="layui-form-item">
        <label class="layui-form-label"  >登录名:</label>
        <div class="layui-input-block">
            <input type="text" name="loginname" id="loginname" lay-verify="loginname" autocomplete="off" placeholder="请输入用户名" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号:</label>
        <div class="layui-input-block">
            <input type="text" id="identity" name="identity" lay-verify="identity" autocomplete="off" placeholder="请输入身份证号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户姓名:</label>
        <div class="layui-input-block">
            <input type="text" name="realname" lay-verify="realname"  placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="1" title="男" checked="">
            <input type="radio" name="sex" value="0" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址:</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="address"  placeholder="请输入地址" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号:</label>
        <div class="layui-input-block">
            <input type="text" id="phone" name="phone" lay-verify="phone"  placeholder="请输入手机号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">密码:</label>
        <div class="layui-input-block">
            <input type="password" name="pwd" lay-verify="pwd"  placeholder="请输入密码" autocomplete="off" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份:</label>
        <div class="layui-input-block">
            <input type="text" id="position" name="position" lay-verify="position"  placeholder="请输入职位" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">职位:</label>
        <div class="layui-input-block">
            <select name="type" lay-search="" id="type" >
                <option value="1">超级管理员</option>
                <option value="2">系统用户</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="adddemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--编辑的form--%>
<form class="layui-form" style="display: none;width: 400px;height: 400px" id="UserEdit" lay-filter="UserEdit" >
    <input type="hidden" name="userid"/>
    <div class="layui-form-item">
        <label class="layui-form-label"  >登录名:</label>
        <div class="layui-input-block">
            <input type="text" name="loginname" id="loginname2" lay-verify="loginname" autocomplete="off"  class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号:</label>
        <div class="layui-input-block">
            <input type="text" id="identity2"  readonly name="identity" lay-verify="identity" autocomplete="off" placeholder="请输入身份证号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户姓名:</label>
        <div class="layui-input-block">
            <input type="text" name="realname" readonly lay-verify="realname"  placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别:</label>
        <div class="layui-input-block">
            <select name="sex" lay-search="" id="sex2" disabled >
                <option value="0">女</option>
                <option value="1">男</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址:</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="address"  placeholder="请输入地址" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号:</label>
        <div class="layui-input-block">
            <input type="text" id="phone2" name="phone" lay-verify="phone"  placeholder="请输入手机号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否可用:</label>
        <div class="layui-input-block">
            <select name="type" lay-search="" id="type2" >
                <option value="0">不可用</option>
                <option value="1">可用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="editdemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--查询所有的角色信息--%>
<div id="divEle3" style="display: none">
    <table id="demo3" lay-filter="test3" ></table>
</div>

<%--头部工具栏--%>
<script type="text/html" id="toolbarDemo2">
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="yes"><i class="layui-icon layui-icon-release"></i>确认分配</button>
        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="no"><i class="layui-icon layui-icon-close-fill"></i>取消分配</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-warm" lay-event="uppwd">重置密码</a>
    <a class="layui-btn layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm" lay-event="fenrole">分配角色</a>
</script>
<%--修改密码--%>
<form class="layui-form" style="display: none;width: 400px;height: 400px" id="editpwd" lay-filter="editpwd" >
    <div class="layui-form-item">
        <label class="layui-form-label" style=" width: 120px">旧密码：</label>
        <div class="layui-input-inline">
            <input type="password" name="e_password" id="e_password" lay-verify="pass" placeholder="请输入旧密码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 120px">新密码：</label>
        <div class="layui-input-inline">
            <input type="password" name="new_password" id="new_password" lay-verify="pass" placeholder="请输入新密码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 120px">请确认新密码：</label>
        <div class="layui-input-inline">
            <input type="password" name="again_password" id="again_password" lay-verify="pass" placeholder="请再次输入密码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center">
        <button class="layui-btn" type="button" lay-submit lay-filter="formDemo" id="commit">确定修改</button>
    </div>
</form>

<script>
    layui.use(['table','form','jquery','layer','element'], function() {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        var upload = layui.upload;
        var element = layui.element;

        //监听头部工具栏
        table.on('toolbar(test)',function (obj) {
            var event=obj.event;
            var checkStatus = table.checkStatus(obj.config.id);
            if (event==='add'){
                $("#UserAdd")[0].reset();
                layer.open({
                    type:1,
                    area:['400px','400px'],
                    content: $("#UserAdd"),
                    titlel:'添加用户'
                })
            }else if (event==='dels'){
                if (checkStatus.data.length>0){
                    var data = checkStatus.data;
                    var flag=1;
                    var aaa=${sessionScope.user.type};
                    $.each(data,function (i) {
                        if(1!=aaa) {
                            flag = 0;
                            layer.msg("对不起,你没有权限")
                        }
                    })
                    if (flag==1){
                        layer.confirm('进行批量删除吗？', function(index){
                            $.ajax({
                                url: "${pageContext.request.contextPath}/user/dels",
                                type:"post",
                                dataType: "json",
                                // contentType:'application/json;charset=utf-8',
                                data:{"user":JSON.stringify(data)},
                                //将一个 JavaScript 对象或值转换为 JSON 字符串
                                success: function (result) {
                                    layer.closeAll();
                                    if (result.code==2000) {
                                        //重载表格
                                        table.reload("test");
                                        layer.msg(result.msg);
                                    } else {
                                        layer.msg(result.msg);
                                    }
                                }
                            });
                        });
                    }

                } else {
                    alert("请选择要批量删除的用户！")
                }
            }
        });

        ////监听行工具事件
        table.on('tool(test)',function (obj) {
            var data=obj.data
                ,$=layui.jquery
                ,form=layui.form;

            if(obj.event==='update'){
                if(data.userid==${sessionScope.user.userid} || 1==${sessionScope.user.type}){
                    layer.open({
                        type: 1
                        ,id:'userId'//防止重复弹出
                        , content:$('#UserEdit')
                        ,btnAlign:'c'//按钮居中
                        ,area:['360px','400px']
                        ,shade:0 //不显示遮罩
                    });
                    //回显数据到表单，Staff对应的是form表单里的lay-filter的值
                    form.val('UserEdit',data);
                }else {
                    layer.msg("你没有权限这样做！！")
                }
            }else if (obj.event==='uppwd'){
                if(data.userid==${sessionScope.user.userid}){
                    layer.open({
                        type: 1
                        ,id:'pwdid'//防止重复弹出
                        , content:$('#editpwd')
                        ,btnAlign:'c'//按钮居中
                        ,area:['400px','400px']
                        ,shade:0 //不显示遮罩
                    });
                }else {
                    layer.msg("自己只能修改自己的密码！！")
                }
            }else if (obj.event==='del'){
                if( 1==${sessionScope.user.type}){
                    layer.confirm('真的要删除行么', function(index){
                        $.ajax({
                            url: "${pageContext.request.contextPath}/user/deleteUser",
                            data: obj.data,
                            type: "post",
                            success: function(result){
                                if(result.code===2000){
                                    //关闭弹窗
                                    layer.closeAll();
                                    //弹出一号表情
                                    layer.msg(result.msg,{icon: 1});
                                    //刷新表格
                                    table.reload('test');
                                }else{
                                    layer.msg(result.msg,{icon:5});
                                }
                            },
                            dataType:"json"
                        })
                    })
                }else {
                    layer.msg("你没有这个权限！！")
                }
            }else if (obj.event==='fenrole'){
                if (1==${sessionScope.user.type}){
                    var userid=data.userid;
                layer.open({
                    type: 1,
                    content: $("#divEle3",userid), //这里content是一个普通的String
                    area: ['780px' ,'400px'],
                    offset: 't',
                });
            }else {
                    layer.msg("你没有这个权限！！")
                }
            }
        });


//监听头部工具栏

        table.on('toolbar(test3)', function(obj){
            var event=obj.event;
            if (event==='yes'){
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var allrole = JSON.stringify(data);
                if (data.length<=0){
                    layer.msg("请选择要分配的角色");
                }else if (data.length>=2){
                    layer.msg("一个用户只能有一个角色");
                    table.reload("test3");
                }else {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/roleuser/updaterole",
                        data: {"allrole": allrole,"userid":userid},
                        type: "post",
                        success: function(result){
                            if(result.code===2000){
                                //关闭弹窗
                                layer.closeAll();
                                //弹出一号表情
                                layer.msg(result.msg,{icon: 1});
                                //刷新表格
                                table.reload('test3');
                            }else{
                                layer.msg(result.msg,{icon:5});
                            }
                        }
                    })
                }
            }else if (event==='no'){
                table.reload('test3');

            }
        });
        table.render({
            id:"test3",
            //elem element 元素---》表格的容器
            elem: '#demo3'//table标签的id属性值
            //,height: 312//整个表格的高度
            ,url: '${pageContext.request.contextPath}/role/AllRole' //数据接口
            ,toolbar: '#toolbarDemo2' //开启头部工具栏，并为其绑定左侧模板
            , title: '角色分配'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'roleid', title: 'ID'}
                ,{field: 'rolename', title: '角色名称'}
                ,{field: 'roledesc', title: '角色备注'}
            ]],
            page:true,//开启分页
            limit:5,//默认每页显示的条数
            limits:[5,10,15,20],//自定义分页条数的选择
        });

        //校验输入的新密码要跟再次输入的密码一样
        $("#form #again_password").blur(function () {
            if ($("#new_password").val() != $("#again_password").val()) {
                layer.msg("两次输入的密码不一样", {icon: 5})

            }
        })

        //修改密码
        $("#commit").click(function () {
            if ($("#new_password").val() != $("#again_password").val() || $("#e_password").val()!=${sessionScope.user.pwd}) {
                layer.msg("密码格式错误或者密码与原密码不符！", {icon: 5})
            }else {
                $.ajax({
                    url: '${pageContext.request.contextPath}/user/updatePassword',
                    data: {pwd  : $("#new_password").val(),userid:"${sessionScope.user.userid}"},
                    dataType: "json",
                    type: 'post',
                    success: function (result) {
                        if (result.code == 2000) {
                            layer.msg(result.msg, {icon: 1})
                            top.window.location.replace("${pageContext.request.contextPath}/login.jsp")
                        } else {
                            layer.msg(result.msg, {icon: 5})
                        }
                    }
                });
            }
        })


        //修改
        form.on('submit(editdemo)',function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/edit",
                data:data.field,
                type:"post",
                success: function(result){
                    if(result.code===2000){
                        //关闭弹窗
                        layer.closeAll();
                        //弹出一号表情
                        layer.msg(result.msg,{icon: 1});
                        //刷新表格
                        table.reload('test');
                    }else{
                        layer.msg(result.msg,{icon:5});
                    }
                },
                dataType:"json"
            })

        });


        //添加提交
        form.on('submit(adddemo)',function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/userAdd",
                data:data.field,
                type:"post",
                success: function(result){
                    if(result.code===2000){
                        //关闭弹窗
                        layer.closeAll();
                        //弹出一号表情
                        layer.msg(result.msg,{icon: 1});
                        //刷新表格
                        table.reload('test');
                    }else{
                        layer.msg(result.msg,{icon:5});
                    }
                },
                dataType:"json"
            })
        });


        //添加表单的搜索监听事件
        form.on('submit(search)',function(data){
            //表格数据重新渲染。
            table.reload('test',{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: { //按照什么进行查找
                    "realname": data.field.realname,
                    "loginname": data.field.loginname,
                    "address": data.field.address,
                    "phone": data.field.phone,
                    "identity": data.field.identity,
                    "sex": data.field.sex
                }
            })
        });

        table.render({
            elem: '#test'
            // ,id:"test"
            , url: '${pageContext.request.contextPath}/user/listByCondition'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , title: '员工数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'userid', title: 'ID', width:40, sort: true }
                , {field: 'realname', title: '用户姓名'}
                , {field: 'loginname', title: '登录名' }
                , {field: 'identity', title: '身份证号'}
                , {field: 'phone', title: '用户电话'}
                , {field: 'address', title: '用户地址'}
                , {field: 'sex', title: '性别',templet:function (c) {
                        return c.sex==1?"男":"女";
                    }}
                , {field: 'available', title: '是否可用',templet:function (c) {
                        return c.available==1?"可用":"不可用";
                    }}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo',width:400}
            ]]
            , page: true
            , limit: 4 //默认每页显示5条
            , limits:[2,4,10,15]
        });
    });
</script>
</body>
</html>
